﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MISAPP.Classify.DataAccess;
using MISAPP.MISInterface;

namespace MISAPP.Classify.Service
{
    public class Service_IMG001:Service_Matrix,iAutoComplete
    //public class Service_IMG001 :  iAutoComplete
    {

        DA_IMG001 img = null;
        DA_IMG001 IMG
        {
            get
            {
                if (img == null)
                {
                    img = new DA_IMG001();
                }
                return img;
            }
        }


        #region iAutoComplete Members

        public System.Data.DataTable GetAutoCompleteTable(string p_qry)
        {
            if (string.Empty.Equals(p_qry))
            {
                return null;
            }
            IMG.ResetCriteria();
            return IMG.GetIM001Accno(p_qry);
        }

        #endregion

        public Service_IMG001()
        {
            dataService = "MISAPP.Classify.DataAccess.DA_IMG001";
        }


        public bool Insert(string p_item,string p_accno)
        {
            return IMG.Insert(p_item, p_accno);
        }

        public bool Del(string p_item, string p_accno,string p_seq)
        {
            return IMG.Del(p_item, p_accno,p_seq);
        }

        public DataTable Query(string p_item, string p_accno)
        {
            return IMG.GetPartialData(p_item, p_accno);
        }

        public string[] GetCriteriaArray()
        {
           return IMG.GetDistinctField();
        }

        public DataSet GetCriteriaData()
        {
            return IMG.GetCriteria();
        }

        public DataTable GetSummaryData(System.Collections.ArrayList p_criteria)
        {
            /*
             * 1.YYYY 要轉成民國年的1到12月
             * 2.項目
             */
            string twYM = p_criteria[1] as string;
            string[] twYMs = getTWYM(twYM);
            List<iSelectList> criteriaList = p_criteria[0] as List<iSelectList>;
            IMG.ResetCriteria();
            foreach (SelectList SL in criteriaList)
            {
                IMG.AddCriteria(SL.GetField(), IMG.GetInString(SL.GetReverse())
                    , SL.GetListArray());
            }
            return IMG.GetSummaryData(twYMs);
        }

        public DataTable GetDetailData(System.Collections.ArrayList p_criteria)
        {
            Dictionary<string, string> fldList = p_criteria[0] as Dictionary<string, string>;
            return IMG.GetDetailData(fldList);
        }

        protected string[] getTWYM(string p_yyyy)
        {
            List<string> yms = new List<string>();
            int yyyy = Convert.ToInt16(p_yyyy) - 1911;
            string mm,m;
            mm = "0{0}";
            for(int i=1;i<13;i++)
            {
                m = string.Format(mm, i.ToString());
                m = m.Length.Equals(3) ? m.Substring(1) : m;
                yms.Add(string.Format("{0}{1}", yyyy.ToString(), m));
            }
            return yms.ToArray<string>();
        }

        


    }
}
